package databasetest;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Update extends JInternalFrame {
	
	Connection conn;
	DatabaseTest dt;
	
	private JLabel label;
	private JTextField code;
	private JLabel label_1;
	private JTextField ea;
	private JButton button;
	private JLabel status;
	private JTextField serial;
	private JLabel lblNewLabel;
	private JButton btnNewButton_1;
	
	
	public Update(Connection c, DatabaseTest dt) {
		super("Update", true, true, true, false);
		
		this.conn = c;
		this.dt = dt;
		
		setBounds(450, 0, 354, 216);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getCode());
		getContentPane().add(getLabel_1());
		getContentPane().add(getEa());
		getContentPane().add(getButton());
		getContentPane().add(getStatus());
		getContentPane().add(getSerial());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getBtnNewButton_1());
		
	}
	
	
		public void setDate(String s, String c, String e){
			serial.setText(s);
			code.setText(c);
			ea.setText(e);
		}
	

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setBounds(12, 40, 30, 20);
		}
		return label;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(81, 41, 116, 21);
		}
		return code;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량");
			label_1.setBounds(12, 70, 30, 20);
		}
		return label_1;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(81, 71, 116, 21);
		}
		return ea;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					int eaa = Integer.parseInt(ea.getText());
					String sql = "update ptbl set code=?, ea=?" + " where serial=?";
					
					try{
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, eaa);
						pstmt.setInt(3, s);
						
						pstmt.executeUpdate();
						pstmt.close();
						
						status.setText("수정되었습니다.");
						
						dt.select.setList();
					}catch(Exception ex){
						
					}
				}
			});
			button.setBounds(81, 101, 97, 23);
		}
		return button;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력해 주세요.");
			status.setOpaque(true);
			status.setBackground(Color.WHITE);
			status.setBounds(12, 152, 314, 25);
		}
		return status;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setBounds(81, 10, 116, 21);
			serial.setColumns(10);
		}
		return serial;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("시리얼");
			lblNewLabel.setBounds(12, 10, 43, 20);
		}
		return lblNewLabel;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("검색");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					int s = Integer.parseInt(serial.getText());
					
					String sql = "select * from ptbl where serial = ?";
					
					try{
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						
						ResultSet rs = pstmt.executeQuery();
						
						if(rs.next()){
							
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getString("ea"));
							
							status.setText(serial.getText() + "입니다.");
						}else{
							status.setText("데이터가 없습니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						
						rs.close();
						pstmt.close();
						
						
					}catch(Exception ex){
						
						
					}
				}
			});
			btnNewButton_1.setBounds(209, 9, 97, 23);
		}
		return btnNewButton_1;
	}
}
